﻿using Google.Protobuf;
using MP_Game.Protos.V1;
using SqlSugar;
namespace MP_VerifyServer
{
    internal class VerifyServer
    {
        static void Main(string[] args)
        {
            LogMsg.Info("验证HTTP服务器启动中...");
            var builder = WebApplication.CreateBuilder(args);
            RegistServices(builder);
            var app = builder.Build();
            // 开发环境下自动创建缺失表
            if (app.Environment.IsDevelopment())
            {
                var db = app.Services.CreateScope().ServiceProvider.GetRequiredService<ISqlSugarClient>();
                db.CodeFirst.InitTables<Account>();
            }

            LoginEndpoint.Map(app);
            RegistEndpoint.Map(app);

            LogMsg.Info("验证HTTP服务器启动成功！");
            app.Run();
        }



        private static void RegistServices(WebApplicationBuilder builder)
        {
            builder.Services.AddScoped<IVerifyService, VerifyService>();

            builder.Services.AddScoped<ISqlSugarClient>(s =>
            {
                var connStr = builder.Configuration.GetConnectionString("MysqlDB");
                return new SqlSugarClient(new ConnectionConfig()
                {
                    DbType = DbType.MySql,
                    ConnectionString = connStr,
                    IsAutoCloseConnection = true,
                });
            });
        }
    }
}
